

package server

import "math"

// Result present a server handler result.
type Result struct {
	// The Response Code: 200 - ok; 300 -error; 301 - not authorized.
	Code int `json:"Code" bson:"Code"`
	// The Response Message
	Msg string `json:"Msg" bson:"Msg"`
	// The Response Data
	Data interface{} `json:"Data,omitempty" bson:"Data,omitempty"`
}

type CgsResult struct {
	// The Response Code: 200 - ok; 300 -error; 301 - not authorized.
	Code int `json:"code" bson:"code"`
	// The Response Message
	Msg string `json:"msg,omitempty" bson:"msg"`
	// The Response Data
	Data interface{} `json:"data,omitempty" bson:"data,omitempty"`
	// Is ok
	Success bool `json:"success"`
}

func (c *CgsResult) WithData(data interface{}) *CgsResult {
	c.Data = data
	return c
}

func (c *CgsResult) WithMsg(msg string) *CgsResult {
	c.Msg = msg
	return c
}

func OK(msg ...string) *CgsResult {
	result := &CgsResult{
		Code:    200,
		Success: true,
	}
	if len(msg) > 0 {
		result.Msg = msg[0]
	}
	return result
}

func Fail(msg string) *CgsResult {
	return &CgsResult{
		Code:    300,
		Msg: msg,
		Success: false,
	}
}

type PageResult struct {
	CgsResult
	PageNo	int64		`json:"pageNo" bson:"pageNo"`
	PageSize int64	`json:"pageSize" bson:"pageSize"`
	TotalCount int64	`json:"totalCount" bson:"totalCount"`
	TotalPage int64	`json:"totalPage,omitempty" bson:"totalPage,omitempty"`
}

func (p *PageResult) CalculateTotalPage() {
	if p.PageSize == 0 {
		p.TotalPage = 0
		return
	}
	p.TotalPage = int64(math.Ceil(float64(p.TotalCount) / float64(p.PageSize)))
}
